console.log(Username);
console.log(itemIds)

var xhr='';

var elQuatities = document.getElementsByClassName("quantity"),
    elListPrice = document.getElementsByClassName("listPrice"),
    elTotal     = document.getElementsByClassName("total"),
    elSubtotal;

var index =elQuatities.length-1;
    console.log(elQuatities[index].value);
    console.log(elQuatities[index].name);


    elQuatities[index].addEventListener('input',function () {
        var itemtid=this.name,
            quantity=this.value;

        updateCart(itemtid,quantity)
    });

function updateCart(itemId,quantity){
    xhr = new XMLHttpRequest();
    xhr.onreadystatechange=process;
    xhr.open("post","updateCart?userId="+Username+"&itemId="+itemId+"&quantity="+quantity,true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send();
}

function process(){
    if(xhr.readyState===4){
        if(xhr.status===200){
            console.log('ok')
            var responseInfo = xhr.responseText;
            console.log(responseInfo);
            var total='';
            var subtotal=0,flag=false;
            var arr =responseInfo.split(",");
            for(var i=0,j=elListPrice.length;i<j;i++){
                console.log(elListPrice[i].attributes.name.nodeValue);
                if(elListPrice[i].attributes.name.nodeValue===arr[0]){
                    total=parseFloat(elListPrice[i].innerText.split("$")[1])*parseFloat(arr[1]);
                }
            }

            for(var i=0,j=elTotal.length;i<j;i++){
                if(elTotal[i].attributes.name.nodeValue===arr[0]){
                    elTotal[i].innerText='$'+total.toFixed(2);
                }
            }

            for(var i=0,j=elTotal.length;i<j;i++){
                subtotal+=parseFloat(elTotal[i].innerText.split("$")[1]);
                flag=true;
            }
            if(flag){
                console.log(elSubtotal);
                elSubtotal.innerText='Sub Total: '+'$'+subtotal.toFixed(2);
            }
        }
    }
}